package com.spc.car.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.entity.base.BaseArea;
import com.spc.car.data.service.base.BaseAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Pattern;
import java.util.List;

@Api(tags = "省/市/区域")
@Slf4j
@Validated
@RestController
@RequestMapping("/base")
public class BaseAreaController {

    @Autowired
    BaseAreaService baseAreaService;


    @ApiOperation(value = "获取子区域", notes = "获取子区域")
    @PostMapping("/listSubArea")
    public AjaxResult listSubArea(@Pattern(regexp = "[0-9]{6}", message = "请输入6位区域编码")
                                  @RequestParam(required = false) String parentCode) {
        LambdaQueryWrapper<BaseArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(parentCode), BaseArea::getParentCode, parentCode)
                .isNull(StringUtils.isBlank(parentCode), BaseArea::getParentCode)
                 .orderByAsc(BaseArea::getCode);
        List<BaseArea> list = baseAreaService.list(wrapper);
        return AjaxResult.success(list);
    }
}
